package com.demo.juc.assistutil;

import java.util.concurrent.CountDownLatch;

/**
 * CountDownLatch  控制次序
 * 应用场景：
 * 控制某个线程一定在某些线程后执行
 *
 */
public class CountDownLatchThread {
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(5);
        for (int i = 0; i < 5; i++) {
            new Thread(() -> {
                try {
                    System.out.println("学生" + Thread.currentThread().getName() +"\t 离开图书馆;");
                    latch.countDown();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }, String.valueOf(i)).start();
        }
        latch.await();
        System.out.println("-----------大爷关门!");
    }
}
